//
// Props
//
// Copyright (C) jextra.net.
//
//  This file is part of the JavaProps Library.
//
//  The JavaProps Library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The JavaProps Library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the JavaProps Library; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.props;

import java.util.*;

/**
 * <p>
 * The props package is a convienent way to read and access properties using a standard mechanism
 * well defined mechanism.
 * </p><p>
 * The order in which property keys are searched are:
 * <ul>
 * <li> Property values set via a -D on the command-line (e.g. -Dexample.property=value).
 * <li> Property file specified via a command-line property 'java.properties' (-Djava.properties=filename).
 * <li> Property file in classpath with 'java.properties' name.
 * <li> Property file in ./ (current directory) with name 'java.properties'.
 * <li> Property that has been set in code via {@link #Props.setString}.
 * </ul>
 * </p>
 */
public class Props
{
    // ============================================================
    // Fields
    // ============================================================

    public static final String PROP_CIPHER = "props.cipher";
    public static final String PROP_DB_DRIVER = "props.db.driver";
    public static final String PROP_DB_URL = "props.db.url";
    public static final String PROP_DB_USER = "props.db.user";
    public static final String PROP_DB_PASSWORD = "props.db.password";
    public static final String PROP_DB_TABLE = "props.db.table";

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public static void loadPropertiesFile( String fileName )
    {
        PropsManager.getInstance().loadPropertiesFile( fileName );
    }

    public static void addProperties( String propsName, Properties props )
    {
        PropsManager.getInstance().addProperties( propsName, props );
    }

    /**
     * Used to retrieve a property value.
     *
     * @param key String - the key you would like the value for
     * @return String - returns the value if the key exists, null otherwise
     */
    public static String getString( String key )
    {
        return PropsManager.getInstance().getString( key );
    }


    /**
     * Used to retrieve a value that was loaded from a property file
     * or previously set using setString()
     *
     * @param key String - the key you would like the value for
     * @param defaultValue String - the value you would like returned if key cannot be found
     * @return String - returns the value if the key exists, defaultValue otherwise
     */
    public static String getString( String key, String defaultValue )
    {
        return PropsManager.getInstance().getString( key, defaultValue );
    }

    /**
     * Use to retrieve a value that was loaded from a property file
     * or previously set using setInt()
     *
     * @param key String - the key you would like the value for
     * @return Integer - returns the value if the key exists, null otherwise
     */
    public static Integer getInteger( String key )
    {
        return PropsManager.getInstance().getInteger( key );
    }

    /**
     * Use to retrieve a value that was loaded from a property file
     * or previously set using setInt()
     *
     * @param key String - the key you would like the value for
     * @param defaultValue Integer - the value you would like returned if key cannot be found
     * @return Integer - returns the value if the key exists, defaultValue otherwise
     */
    public static Integer getInteger( String key, Integer defaultValue )
    {
        return PropsManager.getInstance().getInteger( key, defaultValue );
    }

    public static Long getLong( String key )
    {
        return PropsManager.getInstance().getLong( key );
    }

    public static Long getLong( String key, Long defaultValue )
    {
        return PropsManager.getInstance().getLong( key, defaultValue );
    }

    /**
     * Use to retrieve a value that was loaded from a property file
     * or previously set using setDouble()
     *
     * @param key String - the key you would like the value for
     * @return Double - returns the value if the key exists, null otherwise
     */
    public static Double getDouble( String key )
    {
        return PropsManager.getInstance().getDouble( key );
    }

    /**
     * Use to retrieve a value that was loaded from a property file
     * or previously set using setDouble()
     *
     * @param key String - the key you would like the value for
     * @param defaultValue Double - the value you would like returned if key cannot be found
     * @return Double - returns the value if the key exists, defaultValue otherwise
     */
    public static Double getDouble( String key, Double defaultValue )
    {
        return PropsManager.getInstance().getDouble( key, defaultValue );
    }

    /**
     * Use to retrieve a value that was loaded from a property file
     * or previously set using setBoolean()
     *
     * @param key String - the key you would like the value for
     * @return Boolean - returns the value if the key exists, null otherwise
     */
    public static Boolean getBoolean( String key )
    {
        return PropsManager.getInstance().getBoolean( key );
    }

    /**
     * Use to retrieve a value that was loaded from a property file
     * or previously set using setBoolean()
     *
     * @param key String - the key you would like the value for
     * @param defaultValue Boolean - the value you would like returned if key cannot be found
     * @return Boolean - returns the value if the key exists, defaultValue otherwise
     */
    public static Boolean getBoolean( String key, Boolean defaultValue )
    {
        return PropsManager.getInstance().getBoolean( key, defaultValue );
    }

    /**
     * Same as {@link getString( String )} except it assumes the value is store in an encrypted form in
     * properties file.
     */
    public static String getDecipheredString( String key )
        throws PropsException
    {
        return PropsManager.getInstance().getDecipheredString( key );
    }

    /**
     * Same as {@link getString( String, String )} except it assumes the value is store in an 
     * encrypted form in properties file.
     */
    public static String getDecipheredString( String key, String defaultValue )
        throws PropsException
    {
        return PropsManager.getInstance().getDecipheredString( key, defaultValue );
    }

    /**
     * Will print to System.out the list of files that have been loaded by the
     * PropsManager.  They will appear in the order that the system will search
     * them when getX() is called.
     */
    public static void printFileTrace()
    {
        PropsManager.getInstance().printFileTrace();
    }

    /**
     * Returns the property file that was loaded and used to retrieve a
     * value for the specified key.
     * @param key String - the key to return the file for
     * @return String - the location of the key value
     */
    public static String getPropFile( String key )
    {
        return PropsManager.getInstance().getPropFile( key );
    }

    /**
     * Find all property keys with the given prefix. Note that this is a case sensitive
     * comparison.
     *
     * @param prefix The prefix to find the properties for.
     * @return List of property names which match.
     */
    public static List<String> findPropertyKeysByPrefix( String prefix )
    {
        return PropsManager.getInstance().findPropertyKeysByPrefix( prefix );
    }

    public static void setString( String key, String value )
    {
        PropsManager.getInstance().setString( key, value );
    }
}

